home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / keyboard.swg / 0023_Various READ Key-Chars.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-29  |  2.2 KB  |  89 lines

  1. {
  2. Author : GAYLE DAVIS
  3.  
  4. I have seen a number of messages recently about keyboard access.  Here are
  5. some neat FAST routines to use instead of ReadKey (Crt Unit).  Be advised
  6. that in these routines, I add 128 to the HI Byte in order to be able to use
  7. all 256 Characters.  Just remember to add 128 to test For all Function keys.
  8. }
  9.  
  10. Uses
  11.   Dos;
  12.  
  13. Function GetKey (Var Key : Word) : Boolean; Assembler;
  14. { determine if key pressed and return it as a Word }
  15. { if Lo(key) = 0 and Hi(key) <> 0 then we have a FN key ! }
  16. Asm
  17.   MOV     AH, 1
  18.   INT     16H
  19.   MOV     AL, 0
  20.   JE      @@1
  21.   xor     AH, AH
  22.   INT     16H
  23.   LES     DI, Key
  24.   MOV     Word PTR ES : [DI], AX
  25.   MOV     AL, 1
  26.  @@1 :
  27. end;
  28.  
  29. Function GetChar (Var Key : Char) : Boolean;
  30. { determine if key pressed and return it as a Char}
  31. Var
  32.   c : Word;
  33. begin
  34.   Key := #0;
  35.   if GetKey (c) then
  36.   begin
  37.     GetChar := True;
  38.     if (LO (c) = 0) and (HI (c) <> 0) then
  39.       Key := CHR ( HI (c) + 128 )  { add 128 For FN keys }
  40.     else
  41.       Key := CHR (LO (c) );
  42.   end
  43.   else
  44.     GetChar := False;
  45. end;
  46.  
  47. Function KeyReady : Char;
  48. { looks For and PEEKS at Char but DOES not read it out of buffer }
  49. { returns the Char it finds or #0 if no Character waiting        }
  50. Var
  51.   Regs : Registers;
  52.   Key  : Byte;
  53. begin
  54.   Regs.AH := 1;                          { determine if a key has been }
  55.   INTR ( $16, Regs );                    { converted to a key code     }
  56.   if ( Regs.Flags and FZERO = 0 ) then
  57.   begin                          { yes, Character now in keyboard buffer }
  58.                                  { determine what it is }
  59.     if ( Regs.AL = 0 ) then
  60.       Key := Regs.AH + 128
  61.     else
  62.       Key := Regs.AL;
  63.   end
  64.   else
  65.     Key := 0;
  66.   KeyReady := CHR (Key);
  67. end;
  68.  
  69. Procedure ClearKeyBuffer;
  70. Var
  71.   Regs : Registers;
  72. begin
  73.   Regs.AH := 0;                { Clear ENTIRE keyboard }
  74.   INTR ( $16, Regs );          { buffer via the BIOS   }
  75. end;
  76.  
  77. Function AnyKeyPressed (Ch : Char; Clear : Boolean) : Boolean;
  78. { Check if a Character is present in buffer, and optionally clears it }
  79. Var
  80.   Key  : Char;
  81.   Regs : Registers;
  82.  
  83. begin
  84.   Key := KeyReady;
  85.   AnyKeyPressed := (Key = Ch);
  86.   if (Key = Ch) and Clear then
  87.     ClearKeyBuffer;
  88. end;
  89.